/**
 *
 * @Author 董熠涛
 * @Date 2025/6/16 20:22
 */
package example.springboot.service;

import example.springboot.entity.StudyTask;
import example.springboot.entity.Word;
import example.springboot.mapper.StudyTaskMapper;
import example.springboot.service.WordbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class StudyTaskService {

    @Autowired
    private StudyTaskMapper studyTaskMapper;

    @Autowired
    private WordbookService wordbookService;

    /**
     * 获取或创建今日学习任务
     */
    public StudyTask getOrCreateTodayTask(Integer userId, Integer wordbookId) {
        String today = LocalDate.now().toString();
        StudyTask task = studyTaskMapper.findByUserIdAndDate(userId, today);
        
        if (task == null) {
            // 创建今日任务
            task = new StudyTask();
            task.setUserId(userId);
            task.setWordbookId(wordbookId);
            task.setTargetWords(20); // 默认20个单词
            task.setFinishedWords(0);
            task.setTaskDate(Date.valueOf(LocalDate.now()));
            task.setStatus("pending");
            studyTaskMapper.insert(task);
        }
        
        return task;
    }

    /**
     * 获取今日任务需要学习的单词
     */
    public List<Word> getTodayTaskWords(Integer userId, Integer wordbookId) {
        StudyTask task = getOrCreateTodayTask(userId, wordbookId);
        
        // 获取词书中的单词
        List<Word> allWords = wordbookService.getWordsByWordbookId(wordbookId);
        
        // 随机选择目标数量的单词
        int targetCount = task.getTargetWords() - task.getFinishedWords();
        if (targetCount <= 0 || allWords.isEmpty()) {
            return new ArrayList<>();
        }
        
        if (allWords.size() <= targetCount) {
            return allWords;
        }
        
        // 随机选择
        Random random = new Random();
        List<Word> selectedWords = new ArrayList<>();
        List<Integer> selectedIndices = new ArrayList<>();
        
        while (selectedWords.size() < targetCount) {
            int index = random.nextInt(allWords.size());
            if (!selectedIndices.contains(index)) {
                selectedIndices.add(index);
                selectedWords.add(allWords.get(index));
            }
        }
        
        return selectedWords;
    }

    /**
     * 更新任务进度
     */
    public void updateTaskProgress(Integer userId) {
        String today = LocalDate.now().toString();
        StudyTask task = studyTaskMapper.findByUserIdAndDate(userId, today);
        if (task != null) {
            task.setFinishedWords(task.getFinishedWords() + 1);
            if (task.getFinishedWords() >= task.getTargetWords()) {
                task.setStatus("finished");
            } else {
                task.setStatus("in_progress");
            }
            studyTaskMapper.update(task);
        }
    }

    /**
     * 获取当前任务
     */
    public StudyTask getCurrentTask(Integer userId) {
        return studyTaskMapper.findCurrentTaskByUserId(userId);
    }
}

